package implementation.client;

import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;


public class CEncryptor implements IEncryptor {

	private byte[] key = {0x54,0x2e,0x6a,-122,0x7A,-58,-82,108,97,39,45,91,-94,-12,42,10};
	private MessageDigest md;
	private Cipher cipher;
	private SecretKeySpec skey;
	
	public CEncryptor() {
		try {
			this.md = java.security.MessageDigest.getInstance("SHA-1");
			this.cipher = Cipher.getInstance("AES");
		} catch (Exception e) {}
		
		this.skey = new SecretKeySpec(key, "AES");
	}

	@Override
	public String encrypt(String str) {
		// TODO Auto-generated method stub
		return str;
	}
	
	public String hash(String str){
		if (md!=null){
			md.reset();
			md.update(str.getBytes());
			return DatatypeConverter.printBase64Binary(md.digest());
		}
		return null;
	}
	
	public String AESEncrypt(String str) throws Exception{
		
		cipher.init(Cipher.ENCRYPT_MODE, skey);
	    byte[] encrypted = cipher.doFinal(str.getBytes());
	    return DatatypeConverter.printBase64Binary(encrypted);
	}
	
	public String AESDecrypt(String str) throws Exception{
		String ret;
		cipher.init(Cipher.DECRYPT_MODE, skey);
	    byte[] encrypted = DatatypeConverter.parseBase64Binary(str);
	    ret = new String(cipher.doFinal(encrypted));
	    return ret;
	}
	/*
	 * example:
	public static void main(String[] args){
		CEncryptor e = new CEncryptor();
		try {
			String test = e.AESEncrypt("hgdhgdhgdhfghgfhgfhgfhgfhgf");
			System.out.println(test);
			System.out.println(e.AESDecrypt(test));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	*/
	

}
